/*
 * $RCSfile: OracleURL.java,v $
 * $Revision: 1.1 $
 * $Date: 2017-05-16 $
 *
 * Copyright (C) 2008 Skin, Inc. All rights reserved.
 *
 * This software is the proprietary information of Skin, Inc.
 * Use is subject to license terms.
 */
package com.skin.webcat.database.oracle;

/**
 * <p>Title: OracleURL</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author xuesong.net
 * @version 1.0
 */
public class OracleURL {
    private String host;
    private int port;
    private String serviceName;
    private String userName;
    private String password;

    /**
     * @param args
     */
    public static void main(String[] args) {
        OracleURL oracleURL = OracleURL.parse("jdbc:oracle:thin:@192.168.1.8:1521:ORCL");
        System.out.println(oracleURL);
        System.out.println("host: " + oracleURL.getHost());
        System.out.println("port: " + oracleURL.getPort());
        System.out.println("serviceName: " + oracleURL.getServiceName());
    }

    /**
     * @param url
     * @return OracleURL
     */
    public static OracleURL parse(String url) {
        /**
         * jdbc:oracle:thin:@192.168.1.8:1521:ORCL
         */
        if(!url.startsWith("jdbc:oracle:")) {
            return null;
        }

        int i = url.indexOf('@');
        int j = url.indexOf(':', i + 1);
        int k = url.indexOf(':', j + 1);
        OracleURL oracleURL = new OracleURL();
        oracleURL.setHost(url.substring(i + 1, j));
        oracleURL.setPort(Integer.parseInt(url.substring(j + 1, k)));
        oracleURL.setServiceName(url.substring(k + 1));
        oracleURL.setUserName((String)null);
        oracleURL.setPassword((String)null);
        return oracleURL;
    }

    /**
     * @return the host
     */
    public String getHost() {
        return this.host;
    }

    /**
     * @param host the host to set
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return this.port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * @return the serviceName
     */
    public String getServiceName() {
        return this.serviceName;
    }

    /**
     * @param serviceName the serviceName to set
     */
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return this.userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return this.password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return String
     */
    @Override
    public String toString() {
        return this.toString("jdbc:oracle:thin");
    }

    /**
     * @param type
     * @return String
     */
    public String toString(String type) {
        StringBuilder buffer = new StringBuilder();
        buffer.append(type);
        buffer.append("@");
        buffer.append(this.host);
        buffer.append(":");
        buffer.append(this.port);
        buffer.append(":");
        buffer.append(this.serviceName);
        return buffer.toString();
    }
}
